package me.goldze.mvvmhabit.widget.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.xuexiang.xui.utils.DensityUtils;

import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.utils.DisplayUtils;
import me.goldze.mvvmhabit.utils.ScreenUtils;

public abstract class BaseNiceDialog extends DialogFragment {
    private static final String MARGIN = "margin";
    private static final String WIDTH = "width";
    private static final String HEIGHT = "height";
    private static final String DIM = "dim_amount";
    private static final String BOTTOM = "show_bottom";
    private static final String LEFT = "show_left";
    private static final String RIGHT = "show_right";
    private static final String CANCEL = "out_cancel";
    private static final String ANIM = "anim_style";
    private static final String LAYOUT = "layout_id";
    private static final String WIDTH_BOOLEAN = "width_boolean";

    private int margin;//左右边距
    private int width;//宽度
    private int height;//高度
    private float dimAmount = 0.5f;//灰度深浅
    private boolean showBottom;//是否底部显示
    private boolean showLeft;//是否左边显示
    private boolean showRight;//是否左边显示
    private boolean outCancel = true;//是否点击外部取消
    private boolean width_aboolean = false;
    @StyleRes
    private int animStyle;
    @LayoutRes
    protected int layoutId;
    protected View rootView;

    public abstract int intLayoutId();

    public abstract void convertView(ViewHolder holder, BaseNiceDialog dialog);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NiceDialog);
        layoutId = intLayoutId();

        //恢复保存的数据
//        if (savedInstanceState != null) {
//            margin = savedInstanceState.getInt(MARGIN);
//            width = savedInstanceState.getInt(WIDTH);
//            height = savedInstanceState.getInt(HEIGHT);
//            dimAmount = savedInstanceState.getFloat(DIM);
//            showBottom = savedInstanceState.getBoolean(BOTTOM);
//            showLeft = savedInstanceState.getBoolean(LEFT);
//            showRight = savedInstanceState.getBoolean(RIGHT);
//            outCancel = savedInstanceState.getBoolean(CANCEL);
//            animStyle = savedInstanceState.getInt(ANIM);
//            layoutId = savedInstanceState.getInt(LAYOUT);
//            width_aboolean = savedInstanceState.getBoolean(WIDTH_BOOLEAN);
//        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(null!=rootView){
            return rootView;
        }
        View view = inflater.inflate(layoutId, container, false);
        convertView(ViewHolder.create(view), this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    /**
     * 屏幕旋转等导致DialogFragment销毁后重建时保存数据
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(MARGIN, margin);
        outState.putInt(WIDTH, width);
        outState.putInt(HEIGHT, height);
        outState.putFloat(DIM, dimAmount);
        outState.putBoolean(BOTTOM, showBottom);
        outState.putBoolean(LEFT,showLeft);
        outState.putBoolean(RIGHT,showRight);
        outState.putBoolean(CANCEL, outCancel);
        outState.putInt(ANIM, animStyle);
        outState.putInt(LAYOUT, layoutId);
        outState.putBoolean(WIDTH_BOOLEAN,width_aboolean);
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount;
            //是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM;
                if (animStyle == 0) {
                    animStyle = R.style.DefaultAnimation;
                }
            }

            if (showLeft){
                lp.gravity = Gravity.LEFT;
//                if (animStyle == 0) {
//                    animStyle = R.style.DefaultAnimation;
//                }
            }

            if (showRight){
                lp.gravity = Gravity.RIGHT;
            }

            if (width_aboolean){
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }
            else {
                //设置dialog宽度
                if (width == 0) {
//                lp.width = DensityUtil.getScreenWidth(getContext()) - 2 * Utils.dp2px(getContext(), margin);
                    lp.width = ScreenUtils.getScreenWidth() - DensityUtils.dp2px(BaseApplication.getInstance(), (int) margin);
                } else {
                    lp.width = DisplayUtils.dip2px(BaseApplication.getInstance(),width);
                }
            }


            //设置dialog高度
            if (height == 0) {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.height = DisplayUtils.dip2px(BaseApplication.getInstance(),height);
            }

            //设置dialog进入、退出的动画
            window.setWindowAnimations(animStyle);
            window.setAttributes(lp);
        }
        setCancelable(outCancel);
    }

    public BaseNiceDialog setBoolean(boolean width_aboolean){
        this.width_aboolean = width_aboolean;
        return this;
    }

    public BaseNiceDialog setMargin(int margin) {
        this.margin = margin;
        return this;
    }

    public BaseNiceDialog setWidth(int width) {
        this.width = width;
        return this;
    }

    public BaseNiceDialog setHeight(int height) {
        this.height = height;
        return this;
    }

    public BaseNiceDialog setDimAmount(float dimAmount) {
        this.dimAmount = dimAmount;
        return this;
    }

    public BaseNiceDialog setShowBottom(boolean showBottom) {
        this.showBottom = showBottom;
        return this;
    }

    public BaseNiceDialog setShowLeft(boolean showLeft) {
        this.showLeft = showLeft;
        return this;
    }

    public BaseNiceDialog setShowRight(boolean showRight) {
        this.showRight = showRight;
        return this;
    }

    public BaseNiceDialog setOutCancel(boolean outCancel) {
        this.outCancel = outCancel;
        return this;
    }

    public BaseNiceDialog setAnimStyle(@StyleRes int animStyle) {
        this.animStyle = animStyle;
        return this;
    }

    public BaseNiceDialog show(FragmentManager manager) {
        super.show(manager, String.valueOf(System.currentTimeMillis()));
        return this;
    }
}
